import { NoticeApi, NoticeVO } from '@/api/school/notice'
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { useRouter } from 'vue-router'
export const useNotice = ({ toast }) => {
  const router = useRouter()
  const total = ref(0)
  const noticeList = ref([])
  const queryParams = reactive({
    pageNo: 1,
    pageSize: 10
  })
  const loadmoreState: any = ref('loading')
  const loading = ref(false)
  const getNoticeList = async (isInit = false) => {
    if (isInit) {
      queryParams.pageNo = 1
    }
    loading.value = true
    try {
      const data: any = await NoticeApi.getNoticePage(queryParams)
      console.log(data)
      if (queryParams.pageNo > 1) {
        noticeList.value = noticeList.value.concat(data.list)
      } else {
        noticeList.value = data.list
      }
      total.value = data.total
      if (noticeList.value.length < total.value) {
        loadmoreState.value = 'loading'
      } else {
        loadmoreState.value = 'finished'
      }
      console.log(noticeList.value, '---noticeList.value')
    } catch (error) {
      console.log(error)

      // toast.warning(error)
      loadmoreState.value = 'error'
    } finally {
      loading.value = false
    }
  }

  const handleClickNotice = (notice: NoticeVO) => {
    router.push(`/notice/detail?id=${notice.id}`)
  }
  const handleReachBottom = () => {
    console.log('handleReachBottom')
    if (noticeList.value.length < total.value) {
      loadmoreState.value = 'loading'
      queryParams.pageNo++
      getNoticeList()
    } else {
      loadmoreState.value = 'finished'
    }
  }
  const getCategory = (itemData) => {
    const findItem = getStrDictOptions(DICT_TYPE.SCHOOL_NOTICE_TYPE).find(
      (item) => item.value == itemData?.noticeType
    )
    return findItem?.label || ''
  }

  return {
    noticeList,
    total,
    queryParams,
    loading,
    handleClickNotice,
    getNoticeList,
    handleReachBottom,
    getCategory
  }
}
